<?php
namespace app\index\model;
/**
 * Created by Submint Text.
 * User: hetaoren<452649510@qq.com>
 * Date: 2017/03/10
 * Explain: 银行业务处理
 */
class Banks extends Base{
	/**
	 * 分页
	 */
	public function pageQuery(){
		$where = [];
		$bank_name = input('bank_name');
		if($bank_name)$where['bank_name'] = ['like','%'.$bank_name.'%'];
		$page = $this->where($where)->order('bank_sort asc,bank_id desc')->paginate(input('pagesize/d'))->toArray();
		if(count($page['Rows'])>0){
			foreach ($page['Rows'] as $key => $v){
				if($v['bank_cat'] == 0){
					$page['Rows'][$key]['cat'] = '快速办卡';
				}elseif($v['bank_cat'] == 1){
					$page['Rows'][$key]['cat'] = '提额';
				}elseif($v['bank_cat'] == 2){
					$page['Rows'][$key]['cat'] = '网贷';
				}
				$page['Rows'][$key]['create_time'] = date('Y-m-d',$v['create_time']);
			}
		}
		return $page;
	}

	public function getById($id){
		return $this->get($id);
	}
	/**
	 * 新增
	 */
	public function add(){
		$data = input('post.');
		$data['create_time'] = time();
		HTRUnset($data,'bank_id');
		$result = $this->allowField(true)->save($data);
    	if(false !== $result){
    	    return HTRReturn("新增成功", 1);
    	}else {
            return HTRReturn('新增失败'); 
        }        
	} 
    /**
	 * 编辑
	 */
	public function edit(){
		$data = input('post.');
		HTRUnset($data,'create_time');
	    $result = $this->allowField(true)->save($data,['bank_id'=>(int)$data['bank_id']]);
        if(false !== $result){
        	return HTRReturn("编辑成功", 1);
        }else{
        	return HTRReturn('编辑失败'); 
        }
	}
	/**
	 * 删除
	 */
    public function del(){
	    $id = (int)input('post.id/d');
	    $result = $this->destroy($id);
        if(false !== $result){
        	return HTRReturn("删除成功", 1);
		}else{
            return HTRReturn('删除失败');
        }
	}
	/**
	* 修改银行排序
	*/
	public function changeSort(){
		$id = (int)input('id');
		$bank_sort = (int)input('bank_sort');
		$result = $this->setField(['bank_id'=>$id,'bank_sort'=>$goodbank_sorts_sort]);
		if(false !== $result){
        	return HTRReturn("操作成功", 1);
        }else{
        	return HTRReturn($this->getError());
        }
	}
	
    /**
	 * 编辑
	 */
	public function editStatus(){
		$id = input('post.id/d',0);
		$status = input('post.status/d',0);
	    $result = $this->save(['bank_status'=>($status ? 0 : 1)],['bank_id'=>$id]);
        if(false !== $result){
        	return HTRReturn("编辑成功", 1);
        }else{
        	return HTRReturn($this->getError());
        }
	}
}
